<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新品有戏商家合作平台</title>
    <script src="__JS__/jquery.js"></script>
    <link href="__CSS__/boot.css" rel="stylesheet">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <script src="__JS__/layer/layer.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <style>
        tr td{
            text-align: center;
            vertical-align: middle!important;
        }
    </style>
</head>
<body>
{include file="base/header" /}
<div style="height: 1px"></div>
<button class="layui-btn" onclick="add({$bid})">添加购物车</button>
<button class="layui-btn layui-btn-normal" onclick="shopping({$bid})">去结算</button>
<div style="float: right; margin-top: 5px;margin-bottom: 5px" class="box-title " >
    <form class="form-inline" action="{:url('online/index/goods')}" method="get">
        <input type="hidden" name="bid" value="{$bid}"/>
        <input type="text" name="title" value="<?php if(isset($title)) echo $title ?>" placeholder="商品名称" class="form-control" id="exampleInputEmail3" >
        <input type="submit" class="btn btn-default" value="搜索"/>
        <input type="button" onclick="res({$bid})" value="全部显示" class="btn btn-default"/>
    </form>
</div>
<table class="layui-table " id="test_table">
    <thead>
    <tr>
        <td>请选择</td>
        <td>商品名称</td>
        <td>单价</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    {foreach $result as $val}
    <tr id="{$val.id}">
        <td><input type="checkbox" name="check"/></td>
        <td style="display: none">{$val.id}</td>
        <td>{$val.pname}</td>
        <td>{$val.purchaseprice}</td>
        <td>
            <button type="button" onclick="remove({$val.id})"  class="btn btn-warning btn-xs">下架</button>
        </td>
    </tr>
    {/foreach}
    </tbody>
</table>
{$result->render()}
</body>
<script>
    //全部显示
    function res(bid) {
        window.location.href = "{:url('online/index/goods')}?bid="+bid;
    }
    //添加购物车
    function add(bid) {
        var box = $("input[name=check]:checked").val();
        if(!box){
            layer.alert('请选择需要邮寄的商品');
            return;
        }
        var single = [];
        var rows = document.getElementById("test_table").rows;
        var check = document.getElementsByName("check");
        var  table = document.getElementById("test_table");
        for(var i=0;i<check.length;i++)
        {
            if(check[i].checked){
                var row = check[i].parentElement.parentElement.rowIndex;
                single.push(rows[row].cells[1].innerHTML);
            }
        }
        $.ajax({
            url:"{:url('online/index/shopping')}",
            data:"data="+JSON.stringify(single)+"&bid="+bid,
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.success){
                    layer.alert(res.success,{bnt:['确定','取消']},
                        function () {
                            window.location.reload();
                        },function () {
                            window.location.reload();
                        }
                    );
                }
                if(res.error){
                    layer.alert(res.error);
                }
            }
        });
    }
    //去结算
    function shopping(bid) {
        window.location.href = "{:url('online/shopping/index')}?bid="+bid;
    }
    //下架
    function remove(id) {
        $.ajax({
            url:"{:url('online/index/remove')}",
            data:"id="+id,
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.success){
                    layer.msg(res.success,{icon:1,time:1000});
                    $("#"+id).remove();
                }else if(res.error){
                    layer.alert(res.error);
                }
            }
        });
    }
</script>
</html>